package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.service.IGoodsChannelShipService;
import com.caocaog.market.entity.GoodsChannelShip;
import com.caocaog.market.vo.GoodBindChannelsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;

/**
 * @Description
 * @Author Van.Planifolia
 * @Date 2024/1/11
 * @Version 1.0
 */
@RestController
@RequestMapping("/goodsChannelShip")
@Api(tags = "商品频道绑定")
public class GoodsChannelShipController extends JeecgController<GoodsChannelShip, IGoodsChannelShipService> {
    @Resource
    private IGoodsChannelShipService goodsChannelShipService;

    /**
     * 分页列表查询
     *
     * @param goodsChannelShip 查询条件
     * @param pageNo           分页页码
     * @param pageSize         分页大小
     * @param req              请求
     * @return 查询结果
     */
    //@AutoLog(value = "合同信息表-分页列表查询")
    @ApiOperation(value = "商品频道绑定信息-分页列表查询", notes = "商品频道绑定信息-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<GoodsChannelShip>> queryPageList(GoodsChannelShip goodsChannelShip,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        QueryWrapper<GoodsChannelShip> queryWrapper = QueryGenerator.initQueryWrapper(goodsChannelShip, req.getParameterMap());
        Page<GoodsChannelShip> page = new Page<>(pageNo, pageSize);
        IPage<GoodsChannelShip> pageList = goodsChannelShipService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 根据商品ID查询他的频道信息
     *
     * @param goodId 商品ID
     * @return 查询到的当前已经绑定的商品ID
     */
    @ApiOperation(value = "商品频道绑定信息-根据商品ID查询频道信息", notes = "商品频道绑定信息-根据商品ID查询频道信息")
    @GetMapping(value = "/getByGoodId")
    public Result<?> getByGoodId(@RequestParam String goodId) {
        GoodsChannelShip goodsChannelShip = goodsChannelShipService.getOne(new LambdaQueryWrapper<GoodsChannelShip>().eq(GoodsChannelShip::getGoodsId, goodId));
        return Result.OK("查询成功！", goodsChannelShip == null ? null : goodsChannelShip.getChannelId());
    }

    /**
     * 添加
     *
     * @param goodsChannelShip 被添加的信息
     * @return 添加结果
     */
    @AutoLog(value = "商品频道绑定信息-添加")
    @ApiOperation(value = "商品频道绑定信息-添加", notes = "合同信息表-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody GoodsChannelShip goodsChannelShip) {
        goodsChannelShipService.remove(new LambdaQueryWrapper<GoodsChannelShip>().eq(GoodsChannelShip::getGoodsId, goodsChannelShip.getGoodsId()));
        goodsChannelShipService.saveGoodChannelShip(goodsChannelShip);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param goodsChannelShip 商品频道绑定信息
     * @return 执行结果
     */
    @AutoLog(value = "商品频道绑定信息-编辑")
    @ApiOperation(value = "商品频道绑定信息-编辑", notes = "商品频道绑定信息-编辑")
    @RequiresPermissions("contractBase:bee_contract_base:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody GoodsChannelShip goodsChannelShip) {
        goodsChannelShipService.updateById(goodsChannelShip);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id 被删除的ID
     * @return 删除结果
     */
    @AutoLog(value = "商品频道绑定信息-通过id删除")
    @ApiOperation(value = "商品频道绑定信息-通过id删除", notes = "商品频道绑定信息-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        goodsChannelShipService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids 被删除的IDS
     * @return 删除结果
     */
    @AutoLog(value = "合同信息表-批量删除")
    @ApiOperation(value = "合同信息表-批量删除", notes = "合同信息表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        goodsChannelShipService.removeByIds(Collections.singletonList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id 被查询的ID
     * @return 查询结果
     */
    //@AutoLog(value = "合同信息表-通过id查询")
    @ApiOperation(value = "合同信息表-通过id查询", notes = "合同信息表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        GoodsChannelShip goodsChannelShip = goodsChannelShipService.getById(id);
        if (goodsChannelShip == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(goodsChannelShip);
    }

    /**
     * 商品绑定多频道,当传传入频道为空的时候清空绑定信息
     *
     * @param goodBindChannelsVO 绑定评到必传的信息
     * @return 绑定结果
     */
    @ApiOperation("商品绑定频道，一次多绑定")
    @PostMapping("/goodBindChannels")
    public Result<?> goodBindChannels(@RequestBody @Validated GoodBindChannelsVO goodBindChannelsVO) {
        goodsChannelShipService.goodBindChannels(goodBindChannelsVO);
        return Result.OK("操作成功！");
    }

    /**
     * 清除该频道下的所有商品绑定
     *
     * @param channelId 频道ID
     * @return 执行结果
     */
    @ApiOperation("清除该频道下的所有商品绑定")
    @DeleteMapping("/clearThisChannelGoods")
    public Result<?> clearThisChannelGoodsBind(@RequestParam String channelId) {
        goodsChannelShipService.clearThisChannelGoodsBind(channelId);
        return Result.OK("操作完毕！");
    }

}
